 /*
 * KIMWONSORK@GMAIL.COM
 * COPYRIGHT CRAYONSOFT All rights reserved.
 * 2013오후 8:16:48
 * kws
 */

package com.crayon.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import net.fortuna.ical4j.model.DateTime;
import net.fortuna.ical4j.model.Period;
import net.fortuna.ical4j.model.TimeZoneRegistry;
import net.fortuna.ical4j.model.TimeZoneRegistryFactory;


/**
 * 
 */
public class UTILDate {

	public static Calendar getCaldarInitFirstDay(Date date){
		Calendar cal   = Calendar.getInstance();
		if(date!=null){
			cal.setTime(date);
		}
		cal.set(Calendar.AM_PM, Calendar.AM);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.HOUR, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);

		return cal;
	}
	
	public static int getDAYIntValue(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.DAY_OF_MONTH);
	}

	public static Date getInitDateMonthFirstDay(Date date){
		return getCaldarInitFirstDay(date).getTime();
	}
	public static Date getCalendarInitSSMMSS(Date date){
		Calendar cal   = Calendar.getInstance();
		cal.setTimeInMillis(date.getTime());
		cal.set(Calendar.AM_PM, Calendar.AM);
		cal.set(Calendar.HOUR, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	public static Date getCalendarEndSSMMSS(Date date){
		Calendar cal   = Calendar.getInstance();
		cal.setTimeInMillis(date.getTime());
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		cal.set(Calendar.MILLISECOND, 999);
		return cal.getTime();
	}

	public static Date getNextMONTH(Date date,int next){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, next);
		return cal.getTime();
	}

	

	public static Date getNextHour(Date date,int next){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.HOUR_OF_DAY, next);
		return cal.getTime();


	}
	
	
	public static Date getNextDAY(Date date,int next){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_YEAR, next);
		Date datenew = cal.getTime();
		return datenew;

	}	
	public static Date getBeforeDAY(Date date,int next){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_YEAR, -next);
		Date datenew = cal.getTime();
		return datenew;

	}	

	public static Date getNextWeek(Date date,int next){
		Calendar cal = Calendar.getInstance(Locale.KOREA);
		cal.setTime(date);
		cal.add(Calendar.WEEK_OF_MONTH, next);
		Date datenew = cal.getTime();
		return datenew;

	}
	public static Date getNextYear(Date date,int next){
		Calendar cal = Calendar.getInstance(Locale.KOREA);
		cal.setTime(date);
		cal.add(Calendar.YEAR, next);
		Date datenew = cal.getTime();
		return datenew;

	}

	public static int getMonth(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.MONTH);
	}
	public static Date setMonth(Date date,int month){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.MONTH, month);
		return cal.getTime();
	}
	
	public static Date setDay(Date date,int day){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DAY_OF_MONTH, day);
		return cal.getTime();
	}


	public static int getYear(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.YEAR);
	}

	public static Date setYear(Date date,int year){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.YEAR, year);
		return cal.getTime();
	}

	public static Date getMonthIncrease(Date date,int next){
		Calendar cal = Calendar.getInstance(Locale.KOREA);
		cal.setTime(date);
		cal.add(Calendar.MONTH, next);
		return cal.getTime();
	}

	public static Date getDateInitFirstDay(Date date){
		Calendar cal   = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	public static Date getDateInitLastDay(Date date){
		Calendar cal   = Calendar.getInstance();
		cal.setTime(date);
		int lastDay = cal.getActualMaximum(Calendar.DATE);
		cal.set(Calendar.DAY_OF_MONTH, lastDay);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		cal.set(Calendar.MILLISECOND, 999);
		return cal.getTime();
	}
	
	public static Date getDateStartTime(Date date){
		Calendar cal   = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	public static Date getDateEndTime(Date date){
		Calendar cal   = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		cal.set(Calendar.MILLISECOND, 999);
		return cal.getTime();
	}

	public static boolean compareIsToday(Date start, Date end){
		boolean isToday = true;

		String startStr = getMMDD(start);
		String endStr = getMMDD(end);

		if(startStr.equals(endStr)) isToday = true;
		else isToday = false;

		return isToday;
	}

	

	/**
	 * 
	 * @param date
	 * @return
	 * @date 2013. 3. 27.
	 * @author kws
	 */
	public static String getYYYYMMDD(Date date){
		if(date==null) return "";
		return new SimpleDateFormat("yyyyMMdd").format(date);
	}
	
	public static String getYYYYMMDDwithPeriod(Date date){
		if(date==null) return "";
		return new SimpleDateFormat("yyyy.MM.dd").format(date);
	}
	public static String getYYYY(Date date){
		if(date==null) return "";
		return new SimpleDateFormat("yyyy").format(date);
	}
	public static String getYYYYMM(Date date){
		if(date==null) return "";
		return new SimpleDateFormat("yyyy.MM").format(date);
	}

	public static String getDDyyyy(Date date){
		if(date==null) return "";
		return new SimpleDateFormat("MM.yyyy").format(date);
	}
	public static String getHHMM(Date date){
		if(date==null) return "";
		return new SimpleDateFormat("HH:mm").format(date);
	}
	public static String getHHMMSS(Date date){
		if(date==null) return "";
		return new SimpleDateFormat("HHmmSS").format(date);
	}

	public static String getYYYYMDDHHMMSS(Date date){
		if(date==null) return "";
		return new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss").format(date);
	}

	public static String getYYYYMDDHHMMSSA(Date date){
		if(date==null) return "";
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
	}

	public static String getYYYYMDDHHMMSSRaw(Date date){
		if(date==null) return "";
		return new SimpleDateFormat("yyyyMMddHHmmss").format(date);
	}
	public static String getyyyyMMddHHmmss(Date date){
		if(date==null) return "";
		return new SimpleDateFormat("yyyyMMddHHmmss").format(date);
	}


	public static String getMMDD(Date date){
		if(date==null) return "";
		return new SimpleDateFormat("MM.dd").format(date);
	}
	public static String getMM(Date date){
		if(date==null) return "";
		return new SimpleDateFormat("MM").format(date);
	}
	public static String getDayDD(Date date){
		if(date==null) return "";
		return new SimpleDateFormat("dd").format(date);
	}


	public static Date getDateFromStringFormat(String string){
		try {
			if(string.contains("T")){
				string = string.replace("T", "");
				new SimpleDateFormat("yyyyMMddHHmmss").parse(string);
			}
			return new SimpleDateFormat("yyyyMMddHHmmss").parse(string);

		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}
	public static Date getDateFromStringFormatyyyyMMddHHmmSS(String string){
		try {
			return new SimpleDateFormat("yyyyMMddHHmmss").parse(string);

		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}
	public static Date getDateFromStringFormatYYYYMMDDHH(String string){
		try {
			return new SimpleDateFormat("yyyyMMdd").parse(string);

		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}


	//	public static int getHowLongMONTHBetweenDate(Date start,Date end){
	//
	//		long peroid = end.getTime() - start.getTime();
	//		long dayslong =  peroid/(1000*60*60*24);
	//		
	//
	//		return (int)dayslong;
	//	}

	//	public int getDAY_OF_WEEK(Date date){
	//		Calendar cal = Calendar.getInstance();
	//		cal.setTime(date);
	//		switch(cal.get(Calendar.DAY_OF_WEEK)){
	//		case 1:
	//			return Calendar.SUNDAY;
	//			
	//		}
	//	}


	public static int getPeroidMonth(Date start,Date end){
		if(start==null || end==null) return 0;
		int i=0;
		for(i=0;;i++){
			Date tmpDate = getNextMONTH(start, i);
			if(tmpDate.getTime() > end.getTime()){
				break;
			}
		}
		return i;

	}
	public static int getHowLongDayBetweenDate(Date start,Date end){

//		long peroid = end.getTime() - start.getTime();
//		long dayslong =  peroid/TIME_DAY;
//		double howlong= Math.ceil(new Long(dayslong).doubleValue());
//		TRACE.log("dayslong =" +howlong);
//
//		return new Double(howlong).intValue();
		DateTime from = null;
		DateTime to = 	null;
		Period period;
		try {
			from = new DateTime(start.getTime());
			to = 	new DateTime(end.getTime());
			period = new Period(from, to);
			return period.getDuration().getDays();

			
		} catch (Exception e) {
			// TODO: handle exception
		}
		return 0;
	}
	
	public static int getDayofWeeks(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.DAY_OF_WEEK);
	}
	
	public static String getDayofWeeksToString(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		switch(cal.get(Calendar.DAY_OF_WEEK)){
		case Calendar.SUNDAY:return "SUN";
		case Calendar.MONDAY:return "MON";
		case Calendar.TUESDAY:return "TUE";
		case Calendar.WEDNESDAY:return "WED";
		case Calendar.THURSDAY:return "THU";
		case Calendar.FRIDAY:return "FRI";
		case Calendar.SATURDAY:return "SAT";
		}
		return "";
	}
	
	
	public static Date setDayofWeeks(Date date,int dayofWeek){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DAY_OF_WEEK, dayofWeek);
		return cal.getTime();
	}

	public static Date getMonthLastDay(Date startDate){
		Calendar cal = Calendar.getInstance();
		cal.setTime(startDate);
		cal.set(Calendar.DAY_OF_MONTH,1); //현재 달의 일자를 1일로 변경 후
		cal.add(Calendar.MONTH, +1); //현재 달에 한달을 더합니다.
		cal.add(Calendar.DAY_OF_YEAR, -1);// 그 후 그 달에서 하루를 빼주면 현재달이 마지막 날짜가 구해집니다!
		
		return cal.getTime();
	}
	
	public static net.fortuna.ical4j.model.TimeZone getTimeZoneDateTime(){
		net.fortuna.ical4j.model.TimeZone timezone=null;
		try {
			TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry();
			timezone = registry.getTimeZone("Asia/Seoul");
//			timezone = registry.getTimeZone("Australia/Melbourne");
		} catch (Exception e) {
			// TODO: handle exception
		}
		return timezone;
	}
	
	public static net.fortuna.ical4j.model.Date getConvertICal4jDate(java.util.Date fromdate){
		net.fortuna.ical4j.model.Date date = new net.fortuna.ical4j.model.Date();
		try {
			date.setTime(fromdate.getTime());
		} catch (Exception e) {
			// TODO: handle exception
		}
		return date;
	}
	/**
	 * 해당일의 마지막날이(30,31) 지정일(28,29..)의 날수보다 클때 달의 마지막날로 세팅해준다.
	 * @param date
	 * @param standstamp
	 * @return
	 */
	public static Date convertParentDayOnCalendar(Date date,long standstamp){
		Calendar calRoot = Calendar.getInstance();
		Calendar cal = Calendar.getInstance();

		calRoot.setTimeInMillis(standstamp);
		cal.setTime(date);
		int dayofAccountUser = calRoot.get(Calendar.DAY_OF_MONTH);//최초 넘어온 실행일...
		int dayOfArrayList = cal.get(Calendar.DAY_OF_MONTH);//한달 이전/이후가 설정되서 넘어온 값.
		if(dayofAccountUser > dayOfArrayList){//사용자가 원하는 막날보다 해당 달의 날이 부족 할 경우.
			return UTILDate.getMonthLastDay(date);
		}
		
//		cal.set(Calendar.DAY_OF_MONTH,calRoot.get(Calendar.DAY_OF_MONTH));
//		GDB.log(getClass(), "__ROOT_convertParentDayOnCalendar " 
//				+"\tcalRoot.get(Calendar.DAY_OF_MONTH) " + calRoot.get(Calendar.DAY_OF_MONTH)
//				+"\tcal.get(Calendar.DAY_OF_MONTH)" +cal.get(Calendar.DAY_OF_MONTH)
//				+"From :"+UTILDate.getYYYYMMDD(date)+"\tTo:" + UTILDate.getYYYYMMDD(cal.getTime()) +"\tRoot:" 
//				+ UTILDate.getYYYYMMDD(calRoot.getTime()));
		return cal.getTime();
	}	

	
	public static Date setYYYYMMDD(Date date,int year,int month, int dayofMonth){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month);
		calendar.set(Calendar.DAY_OF_MONTH, dayofMonth);
		return calendar.getTime();
	}
	public static final long TIME_SEC=1000;
	public static final long TIME_MIN=TIME_SEC*60; 
	public static final long TIME_HOUR=TIME_MIN*60; 
	public static final long TIME_DAY=TIME_HOUR*24; 

}